package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.fragment.AllFragment;
import com.neusoft.zqygbc.neusoft_wenda.fragment.Fragment_1;
import com.neusoft.zqygbc.neusoft_wenda.fragment.Fragment_2;
import com.neusoft.zqygbc.neusoft_wenda.fragment.Fragment_3;
import com.neusoft.zqygbc.neusoft_wenda.fragment.Fragment_4;
import com.neusoft.zqygbc.neusoft_wenda.fragment.HisAnswerFragment;
import com.neusoft.zqygbc.neusoft_wenda.fragment.HisQuizFragment;
import com.neusoft.zqygbc.neusoft_wenda.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

public class HisCreaterActivity extends BaseAvtivity  implements TabLayout.OnTabSelectedListener  {
    private ImageView hiscreater_return;

    private List<Fragment> list = new ArrayList<>();
    private String title[] = {"提问", "回答"};
    private ViewPager viewPager;
    private TabLayout tabLayout;

    @Override
    void initViews() {
        setLayout(R.layout.activity_his_creater);
        hiscreater_return = (ImageView) findViewById(R.id.hiscreater_return);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout_his);
        viewPager = (ViewPager) findViewById(R.id.viewpager_his);

        list.add(new HisQuizFragment());
        list.add(new HisAnswerFragment());
        /**
         * 设置TabLayout的模式
         * TabLayout具有两个模式
         *  TabLayout.MODE_FIXED        这个也是默认的模式，tab平分屏幕宽度
         *  TabLayout.MODE_SCROLLABLE   意思为可滚动的，每个tab具有固定的宽度，
         *                              当屏幕容不下时可以左右滑动选择页面
         **/
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //设置Viewpager的预加载的条目 间接保存Fragment的状态
        viewPager.setOffscreenPageLimit(1);
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter();
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);

        tabLayout.setTabsFromPagerAdapter(adapter);
        //自定义的tab布局
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            View v = getLayoutInflater().inflate(R.layout.tabhost_tab, null);
            TextView textView = (TextView) v.findViewById(R.id.tab_text);
            textView.setText(title[i]);
            //设置自定义的tab布局
            tab.setCustomView(v);
        }
        //    onTabSelected(tabLayout.getTabAt(1));
        onTabSelected(tabLayout.getTabAt(0));

        tabLayout.setOnTabSelectedListener(this);
    }

    @Override
    void initEvents() {
        hiscreater_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    void initData() {

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition();
        viewPager.setCurrentItem(position, false);

    }


    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter() {
            super(HisCreaterActivity.this.getSupportFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}
